package xin.marcher.wind.process.node;

import lombok.Getter;

import java.util.HashSet;
import java.util.Set;

/**
 * 表示一个流程
 *
 * @author marcher
 */
@Getter
public class ProcessorDefinition {

    /**
     * 流程名称
     */
    private String name;

    /**
     * 初始节点
     */
    private ProcessorNode first;

    /**
     * 关联的业务
     */
    private Set<BizConfig> bizConfigs = new HashSet<>();

    public ProcessorDefinition() {
    }

    public ProcessorDefinition(ProcessorNode first) {
        setFirst(first);
    }

    public void setFirst(ProcessorNode first) {
        this.first = first;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void addBizConfig(BizConfig bizConfig) {
        bizConfigs.add(bizConfig);
    }

    public String toStr() {
        StringBuilder sb = new StringBuilder();
        sb.append("name:").append(name).append("\n");
        sb.append("nodes:\n");
        buildStr(sb, first);
        sb.append("bizConfigs:\n");
        for(BizConfig bizConfig : bizConfigs) {
            sb.append(" ").append(bizConfig.getName()).append("->")
                    .append("businessIdentifier:")
                    .append(bizConfig.getBusinessIdentifier())
                    .append("|")
                    .append("orderType:")
                    .append(bizConfig.getOrderType())
                    .append("\n");
        }
        return sb.toString();
    }

    private void buildStr(StringBuilder sb, ProcessorNode node) {
        for (ProcessorNode processorNode : node.getNextNodes().values()) {
            sb.append(" ").append(node.getName()).append(" -> ").append(processorNode.getName()).append("\n");
            buildStr(sb, processorNode);
        }
    }

    /**
     * 是否匹配业务线 + 订单类型
     */
    public boolean matchBiz(Integer businessIdentifier, Integer orderType) {
        return bizConfigs.contains(new BizConfig(businessIdentifier, orderType));
    }

}
